package com.ctrip.cap.amqp;

import java.util.Properties;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

public class Producer {
	/**
	 * Publish a message to spec queue
	 * 
	 * @param queueName
	 * @param message
	 * @return true or false
	 */
	public static boolean Publish(String queueName, String message) {

		try {
			CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
					"csm-haddop03.dev.sh.ctripcorp.com");
			connectionFactory.setUsername("admin");
			connectionFactory.setPassword("123456");

			RabbitAdmin admin = new RabbitAdmin(connectionFactory);
			Queue queue = new Queue(queueName);
			Properties queueProp = admin.getQueueProperties(queueName);
			if (queueProp != null) {

			} else {
				admin.declareQueue(queue);

			}

			RabbitTemplate template = new RabbitTemplate(connectionFactory);
			template.setRoutingKey(queueName);
			template.setQueue(queueName);

			template.convertAndSend(message);

			return true;

		} catch (Exception e) {
			return false;
		}

	}
}